package com.ffapp.demo.chat;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ffapp.demo.R;
import com.ffapp.demo.basis.BasisActivity;
import com.ffapp.demo.bean.LoginBean;
import com.ffapp.demo.bean.UserInfoBean;
import com.ffapp.demo.bean.UserStateData;
import com.flyco.dialog.widget.NormalDialog;
import com.google.gson.Gson;

import java.util.Calendar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import my.MySharedPreferences;
import my.TimeUtils;


/**
 * @备注 人员状态编辑对话框
 */
public class DialogPeopleStatus extends NormalDialog {


	BasisActivity mContext;

	@BindView(R.id.text_time)
	TextView text_time;
	@BindView(R.id.text_name)
	TextView text_name;
	@BindView(R.id.text_dept)
	TextView text_dept;
	@BindView(R.id.text_mobile)
	TextView text_mobile;
	@BindView(R.id.edit_innner_num)
	EditText edit_innner_num;
	@BindView(R.id.edit_help_num)
	EditText edit_help_num;
	@BindView(R.id.edit_status)
	EditText edit_status;

	Calendar calendarBegin;
	String mTimeFormat = TimeUtils.TIME_FOMART;
	onPeopleStatusEditListener mListener;


	public DialogPeopleStatus(BasisActivity context, onPeopleStatusEditListener listener) {
		super(context);
		mContext = context;
		mListener = listener;
		setCancelable(true);
		setCanceledOnTouchOutside(false);
	}

	@Override
	public View onCreateView() {
//        return super.onCreateView();
		View inflate = LayoutInflater.from(mContext).inflate(R.layout.chat_people_status_dialog, null);
		ButterKnife.bind(this, inflate);
		widthScale(0.9f);
		return inflate;
	}

	@Override
	public void setUiBeforShow() {
		initViews();
		initData();
	}

	private void initViews() {

	}

	private void initData() {
//		updateInfoOld = UserBaseInfoUpdateCache.getFromCache();
		calendarBegin = Calendar.getInstance();
		onTimeBeginChoosed();
		UserInfoBean userInfoBean = LoginBean.getInstance().getUserInfo();
		text_name.setText(userInfoBean.getName());
		text_dept.setText(userInfoBean.getDeptName());
		text_mobile.setText(""+new MySharedPreferences(mContext).getUser());
		edit_help_num.setText(0+"");
		edit_innner_num.setText(0+"");
		edit_status.setText("正常");

	}

//	@OnClick(R.id.text_time)
//	void onTimeClick() {
//		boolean[] typebooleans;
//			typebooleans = new boolean[]{true, true, true, true, true, true};
//		//时间选择器
//		TimePickerView pvTime = new TimePickerBuilder(mContext, new OnTimeSelectListener() {
//			@Override
//			public void onTimeSelect(Date date, View v) {
//				if (calendarBegin== null) {
//					calendarBegin = Calendar.getInstance();
//				}
//				calendarBegin.setTime(date);
//				onTimeBeginChoosed();
//			}
//		})
//				.setType(typebooleans)// 默认全部显示
//				.isDialog(true)
//				.setDate(calendarBegin == null?Calendar.getInstance():calendarBegin)
//				.build();
//		pvTime.show();
//		ViewUtils.hideInput(mContext);
//	}
	void onTimeBeginChoosed() {
		text_time.setText(TimeUtils.getTimeByDate(calendarBegin.getTime(),mTimeFormat));
	}

	@OnClick({R.id.img_close,R.id.btn_cancel})
	void onCloseClick(){
		if(mListener != null){
			mListener.onCancel();
		}
		dismiss();
	}


	@OnClick(R.id.btn_ok)
	void post() {
		if (calendarBegin == null) {
			mContext.showToast(text_time.getHint().toString());
			return;
		}

		String in_num = edit_innner_num.getText().toString().trim();
		if(TextUtils.isEmpty(in_num)){
			mListener.onToast("内功不可为空");
			return;
		}
		String help_num = edit_help_num.getText().toString().trim();
		if(TextUtils.isEmpty(help_num)){
			mListener.onToast("搜救不可为空");
			return;
		}
		String people_status = edit_status.getText().toString().trim();
		if(TextUtils.isEmpty(people_status)){
			mListener.onToast("人员状态不可为空");
			return;
		}

		StringBuilder builder = new StringBuilder();
		builder.append("姓名：").append(text_name.getText().toString()).append("\n")
				.append("单位：").append(text_dept.getText().toString()).append("\n")
				.append("电话：").append(text_mobile.getText().toString()).append("\n")
				.append("时间：").append(text_time.getText().toString()).append("\n")
				.append("内攻：").append(in_num).append("人").append("\n")
				.append("搜救：").append(help_num).append("人").append("\n")
				.append("人员状态：").append(people_status)
				;

		UserStateData userStateData = new UserStateData();
		userStateData.setUsername(text_name.getText().toString());
		userStateData.setDeptname(text_dept.getText().toString());
		userStateData.setPhone(text_mobile.getText().toString());
		userStateData.setTime(text_time.getText().toString());
		userStateData.setNeigong(in_num);
		userStateData.setSoujiu(help_num);
		userStateData.setStatus(people_status);


		Gson gson = new Gson();
		String json = gson.toJson(userStateData);

		if(mListener != null){
			mListener.onPost(json);
			dismiss();
		}

	}



	public interface onPeopleStatusEditListener {
		abstract void onPost(String content);
		abstract void onCancel();
		abstract void onToast(String mes);

	}
}
